package presentacion;

import isg3.GestiondeEventos;
import isg3.Persona;

import java.io.IOException;
import java.util.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


 public class FrontController extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
         String user= "pechuga";
         String passwd = "pechuga";
         List noLogado = new LinkedList();
         List logado = new LinkedList();
         
         private void rellenalistas() {
        	 logado.add("index.jsp");
        	 noLogado.add("index.jsp");
        	 noLogado.add("crearevento.jsp");
        	 noLogado.add("eventoexpandido.jsp");
        	 logado.add("eventoexpandido.jsp");
        	 noLogado.add("eventoexpandido.jsp");
        	 logado.add("firmarevento.jsp");
        	 logado.add("login.jsp");
        	 noLogado.add("login.jsp");
        	 logado.add("registro.html");
        	 noLogado.add("registro.html");
        	 logado.add("votocomentario.jsp");
		}
         
                
               
                public void init() throws ServletException {
            }

            protected void doGet(HttpServletRequest request,
                            HttpServletResponse response) throws ServletException, IOException {
                    processRequest(request, response);

            }

            protected void doPost(HttpServletRequest request,
                            HttpServletResponse response) throws ServletException, IOException {
                    processRequest(request, response);
            }

            public void processRequest(HttpServletRequest request,
                            HttpServletResponse response) throws IOException, ServletException {
            	rellenalistas();
            	String res = request.getParameter("res");
                String idevento= request.getParameter("idevento");
                RequestDispatcher d = null;
            	if (logado(request)) {
                            
                    		System.out.println(res);
                    		System.out.println(idevento);
                    		String resource = res; // Monta la url a la que va con el valor de res
                    		if( idevento != null){// si ha podido recoger idevento 
                            resource = resource+"?idevento="+idevento; // lo concatena ,construyendo una url bien formada res?idevento=4
                    		}
                            d = request.getRequestDispatcher(resource);
                            if(d!=null && logado.contains(res)){
                                    d.forward(request,response);
                            }else{
                            response.sendRedirect("error.html");
                            }
                
                            System.out.println("LOGADO ACCEDIENDO A " + resource);

                    } else {
                    	System.out.println(res);
                    	if(noLogado.contains(res)){
                    		 	d = request.getRequestDispatcher(res);
                    			d.forward(request,response);
                    	}else{
                    		response.sendRedirect("error.html");	
                    	}
                        
                    }

            }

            private boolean logado(HttpServletRequest request) {
                    boolean logado = false;

                    HttpSession session = request.getSession(false);

                    String userForm = request.getParameter("user");
                    String passwdForm = request.getParameter("passwd");

                    if (session == null) {
                            session = request.getSession();
                            if (userForm == null || passwdForm == null
                                            || userForm.length() == 0 || passwdForm.length() == 0) {
                                    logado = false;

                            } else {
                                    if (valido(userForm, passwdForm)) {
                                            logado = true;
                                            session.setAttribute("session.user", userForm);
                                    } else {
                                            logado = false;
                                    }

                            }

                    } else {
                            if ((userForm == null || passwdForm == null)) {
                                    logado = true;
                            } else {
                                    if (valido(userForm, passwdForm)) {
                                            logado = true;
                                            session.setAttribute("session.user", userForm);
                                    } else {
                                            logado = false;
                                    }

                            }
                    }
                    return logado;
            }

            public boolean valido(String userForm, String passwdForm) {
            	List usuarios= GestiondeEventos.getInstance().getPersonas();
            	Iterator it= usuarios.iterator();
            	Persona p=null;
            	boolean enc=false;
            	while (it.hasNext()&&!enc){
            		p=(Persona)it.next();
            		if(p.getUsuario().equals(userForm)){
            			enc=true;
            		}
            	}
            	boolean res = false;
                    res =  passwdForm.equals(p.getContrasena());
                    return res;

            }
 }